package com.xunchi.haichuan.common.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 作者：kongxb<br>
 * 模块名称：公共模块<br>
 * 功能描述：错误处理<br>
 * 维护记录：2009.11.27, kongxb 创建该类
 */
public class ErrorAction extends ActionSupport {

	private static final long serialVersionUID = 4327227676494377675L;

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ErrorAction.class);

	/**
	 * Method execute
	 * 
	 */
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}

		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);

		String key = (String) request.getAttribute("error");
		if (StringUtils.isNotEmpty(key)) {
			addActionError(getText(key));
		}

		response.setStatus(401);

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end");
		}
		return Action.SUCCESS;
	}
}